package dao.impl;

import core.BaseDao;
import dao.TableInfoDao;
import org.apache.commons.dbutils.handlers.BeanHandler;
import pojo.TableInfo;

import java.sql.SQLException;
import java.sql.Timestamp;

public class TableInfoDaoImpl extends BaseDao implements TableInfoDao {


    /**
     * 通过订单id查找餐桌信息
     * @param orderId
     * @return
     */
    @Override
    public TableInfo findTableInfoByOrderId(String orderId) {
        try {
            sql = "SELECT * FROM table_info WHERE table_info_order_id=?";
            return queryRunner.query(sql, new BeanHandler<>(TableInfo.class, rowProcessor), orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new TableInfo();
    }

    /**
     * 通过订单id删除餐桌信息
     * @param orderId
     * @return
     */
    @Override
    public Integer deleteTableInfoByOrderId(String orderId) {

        try {
            sql = "DELETE FROM table_info WHERE table_info_order_id=?";
            return queryRunner.update(sql,orderId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /**
     * 通过餐桌id查找最新餐桌信息
     * @param tableId
     * @return
     */
    @Override
    public TableInfo findTableInfoByTableId(String tableId) {
        try {
            sql = "SELECT * FROM table_info WHERE table_id = ? ORDER BY table_info_id DESC LIMIT 1";
            return queryRunner.query(sql, new BeanHandler<>(TableInfo.class, rowProcessor), tableId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new TableInfo();
    }

    /**
     * 添加预定的餐桌信息
     * @param tableInfo
     * @return
     */
    @Override
    public Integer addBookInfo(TableInfo tableInfo) {
        try {
            sql = "INSERT INTO table_info(table_id, table_info_book_name, table_info_book_phone,table_info_worker_id,table_info_book_time) VALUES (?,?,?,?,?)";
            return queryRunner.update(sql,
                    tableInfo.getTableId(),
                    tableInfo.getTableInfoBookName(),
                    tableInfo.getTableInfoBookPhone(),
                    tableInfo.getTableInfoWorkerId(),
                    tableInfo.getTableInfoBookTime()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加餐桌信息
     * @param tableInfo
     * @return
     */
    @Override
    public Integer add(TableInfo tableInfo) {
        try {
            sql = "INSERT INTO table_info(table_id, table_info_order_id, table_info_setup_time,table_info_worker_id, table_info_num) VALUES (?,?,?,?,?)";
            return queryRunner.update(sql,
                    tableInfo.getTableId(),
                    tableInfo.getTableInfoOrderId(),
                    new Timestamp(System.currentTimeMillis()),
                    tableInfo.getTableInfoWorkerId(),
                    tableInfo.getTableInfoNum()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
